package com.admission.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.GroupRegister;
import com.admission.model.OrderTemp;
import com.admission.model.PayLog;
import com.admission.service.GroupRegisterManager;
import com.admission.service.OrderTempManager;
import com.admission.service.PayLogManager;
import com.admission.util.DateUtils;
import com.admission.util.PageUtils;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class GroupRegisterAction extends CrudActionSupport<GroupRegister> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7620874741038044138L;
	private Page<OrderTemp> page = new Page<OrderTemp>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private OrderTempManager orderTempManager;
	private OrderTemp orderTemp;
	private GroupRegisterManager groupRegisterManager;
	private PayLogManager payLogManager;
	private GroupRegister register;
	private String id;
	private String mobile;
	private String name;
	private String unit;
	private String password;
	private PayLog payLog;
	private String userIdentify;
	private String examineeNo;
	HttpServletRequest request = ServletActionContext.getRequest();

	public Page<OrderTemp> getPage() {
		return page;
	}

	public void setPage(Page<OrderTemp> page) {
		this.page = page;
	}

	public OrderTempManager getOrderTempManager() {
		return orderTempManager;
	}

	public void setOrderTempManager(OrderTempManager orderTempManager) {
		this.orderTempManager = orderTempManager;
	}

	public OrderTemp getOrderTemp() {
		return orderTemp;
	}

	public void setOrderTemp(OrderTemp orderTemp) {
		this.orderTemp = orderTemp;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public PayLogManager getPayLogManager() {
		return payLogManager;
	}

	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}

	public PayLog getPayLog() {
		return payLog;
	}

	public void setPayLog(PayLog payLog) {
		this.payLog = payLog;
	}

	public GroupRegisterManager getGroupRegisterManager() {
		return groupRegisterManager;
	}

	public void setGroupRegisterManager(
			GroupRegisterManager groupRegisterManager) {
		this.groupRegisterManager = groupRegisterManager;
	}

	public GroupRegister getRegister() {
		return register;
	}

	public void setRegister(GroupRegister register) {
		this.register = register;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public GroupRegister getModel() {
		return register;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserIdentify(String userIdentify) {
		this.userIdentify = userIdentify;
	}

	public void setExamineeNo(String examineeNo) {
		this.examineeNo = examineeNo;
	}

	/**
	 * 登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkLogin() throws Exception {
		register = groupRegisterManager.getGroupRegisterByPhone(mobile);
		if (register != null) {
			if (register.getPassword().trim().equals(password)) {
				HttpSession session = request.getSession(false);
				session.setAttribute("GROUP_USER_NAME", register.getName());
				session.setAttribute("USER_PHONE", register.getMobile());
				return this.list();
			} else {
				this.addActionMessage("密码有误");
			}
		} else {
			this.addActionMessage("用户不存在");
		}
		return "examinee-pay-login";
	}

	/**
	 * 转向注册页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {

		return "group-register-reg";
	}

	/**
	 * 保存
	 */
	@Override
	public String save() throws Exception {
		try {
			GroupRegister registerCheck = groupRegisterManager
					.getGroupRegisterByPhone(mobile);
			if (registerCheck != null) {
				Struts2Utils.renderText("existed");
			} else {
				Date date = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				register.setResgisterTime(dateFormat.format(date));
				groupRegisterManager.save(register);
				Struts2Utils.renderText("success");
			}
		} catch (Exception e) {
			Struts2Utils.renderText("fail");
		}
		return null;
	}

	@Override
	public String delete() throws Exception {
		try {
			orderTempManager.deleteById(id);
		} catch (Exception e) {
			this.addActionMessage("删除失败，请联系管理员");
		}
		return this.list();
	}

	@Override
	public String input() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		String mobile = request.getSession().getAttribute("USER_PHONE")
				.toString();
		if (!StringUtils.isEmpty(mobile)) {
			PropertyFilter p1 = new PropertyFilter("EQS_mobile", mobile);
			filters.add(p1);
		}
		page = orderTempManager.getPageByFilter(page, filters);

		return INPUT;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		String mobile = request.getSession().getAttribute("USER_PHONE")
				.toString();
		if (!StringUtils.isEmpty(mobile)) {
			PropertyFilter p1 = new PropertyFilter("EQS_mobile", mobile);
			filters.add(p1);
		}
		page = orderTempManager.getPageByFilter(page, filters);

		return "group-pay-index";
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			register = new GroupRegister();
		} else {
			register = groupRegisterManager.getGroupRegisterById(id);
		}
	}

	public String search() throws Exception {
		payLog = payLogManager.getPayLogByIdNum(userIdentify);
		if (payLog == null)
			this.addActionMessage("此考生不存在，请核对身份证号!");
		else {
			if (payLog.getPayStatus().equals("1")) {
				payLog.setPayStatus("已缴费");
			} else {
				payLog.setPayStatus("未缴费");
			}
		}
		return "group-pay-index";
	}

	public String AddToOrderTemp() throws Exception {
		try {
			payLog = payLogManager.getPayLogByIdNum(userIdentify);
			if (payLog.getPayStatus().equals("1")) {
				this.addActionMessage("该考生已缴费");
				return this.list();
			}
			orderTemp = orderTempManager.getOrderTempByIdNum(userIdentify);
			if (orderTemp != null) {
				this.addActionMessage("该记录已存在");
				return this.list();
			}

			List<PropertyFilter> filters = PropertyFilter
					.buildFromHttpRequest(Struts2Utils.getRequest());
			String mobile = request.getSession().getAttribute("USER_PHONE")
					.toString();
			if (!StringUtils.isEmpty(mobile)) {
				PropertyFilter p1 = new PropertyFilter("EQS_mobile", mobile);
				filters.add(p1);
			}
			page = orderTempManager.getPageByFilter(page, filters);
			List<OrderTemp> orderTempList = page.getResult();
			String orderID = "";
			if (orderTempList.size() > 0) {
				orderID = orderTempList.get(0).getOrderId();
			} else {
				// 集体缴费订单号用手机号后10位加时间时分秒拼接
				orderID = request.getSession().getAttribute("USER_PHONE")
						.toString().substring(1)
						+ DateUtils.getTimeString();
			}
			if (orderTempList.size() == 20) {
				this.addActionMessage("最多添加20个考生");
				return this.list();
			}
			orderTemp = new OrderTemp();
			orderTemp.setOrderId(orderID);
			orderTemp.setMobile(request.getSession().getAttribute("USER_PHONE")
					.toString());
			orderTemp.setExamineeNo(payLog.getExamieeNo());
			orderTemp.setIdnum(payLog.getIdnum());
			orderTemp.setName(payLog.getName());
			orderTemp.setAmount(payLog.getAmount());
			orderTempManager.save(orderTemp);
			this.addActionMessage("添加成功");
		} catch (Exception e) {
			this.addActionMessage("添加失败，请联系管理员");
		}
		return this.list();
	}

}
